package com.eastdigit.system.service;

import com.eastdigit.system.inter.UserRoleMapper;
import com.eastdigit.system.model.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserRoleService extends BaseService implements IUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private DataScopeService dataScopeService;

    public List<UserRole> queryRolesAssignedToUser(String roleId) {
        return userRoleMapper.selectByRoleId(roleId);
    }

    /**
     * 添加用户角色
     * 
     * @param userId
     * @param roleId
     */
    public void addUserRole(String userId, String roleId) {
        UserRole userRole = new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(userId);
        userRoleMapper.insert(userRole);

        // 修改数据范围表
        dataScopeService.updateByUserAddRole(userId, roleId);
    }

    /**
     * 删除用户角色
     * 
     * @param userId
     * @param roleId
     */
    public void delUserRole(String userId, String roleId) {
        // 修改数据范围表，这里要先执行
        dataScopeService.updateByUserDeleteRole(userId, roleId);

        userRoleMapper.deleteByPrimaryKey(roleId, userId);
    }

    /**
     * 删除用户角色
     * 
     * @param userId
     */
    public void delUserRoleOfUser(String userId) {
        userRoleMapper.deleteByUserId(userId);
        // 修改数据范围表
        dataScopeService.updateByUserDelete(userId);
    }

    /**
     * 删除角色
     * 
     * @param roleId
     */
    public void delUserRoleOfRole(String roleId) {
        userRoleMapper.deleteByRoleId(roleId);
    }
}
